package cn.raven.org.controller;

import cn.raven.basic.utils.AjaxResult;
import cn.raven.basic.utils.PageList;
import cn.raven.org.domain.Department;
import cn.raven.org.query.DepartmentQuery;
import cn.raven.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depart")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: findAll
    * @Description: 查询所有
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 18:04
    * @Parameters: []
    * @Return java.util.List<cn.raven.org.domain.Department>
      */
    @GetMapping
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
    * @Title: findOne
    * @Description: 根据id查询
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 18:06
    * @Parameters: [id]
    * @Return cn.raven.org.domain.Department
      */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department findOne(@ApiParam(value="ID属性",required = true)@PathVariable Long id){
        return departmentService.findOne(id);
    }

    /**
    * @Title: del
    * @Description: 根据id删除
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 18:08
    * @Parameters: [id]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable Long id){
        departmentService.del(id);
        return AjaxResult.me();
    }

    /**
    * @Title: save
    * @Description: 新增或修改
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 18:10
    * @Parameters: [department]
    * @Return cn.raven.basic.utils.AjaxResult
      */
    @PutMapping
    public AjaxResult save(@RequestBody Department department){
        try {
            if(department.getId() !=null){
                departmentService.update(department);
            }else {
                departmentService.add(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("G了呀");
        }
    }
    
    /** 
    * @Title: queryData
    * @Description: 分页+高级查询
    * @Author: king
    * @Version: 1.0
    * @Date:  2022/6/24 18:14
    * @Parameters: [departmentQuery]
    * @Return cn.raven.basic.utils.PageList<cn.raven.org.domain.Department>
      */
    @PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryData(departmentQuery);
    }
}
